﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using CoffeeSql.Core;
using CoffeeSql.Core.Attributes;
using CoffeeSql.Core.SqlDataAccess;
using System.Data.Common;
using System.Data.SqlClient;
using CoffeeSql.Core.EntityDesign;
using CoffeeSql.Mysql;
using CoffeeSql.Core.SqlStatementManagement;
using CoffeeSql.Oracle.DbContext;

namespace test
{
    class Program
    {
        public static string oracleConnStr = "Data Source=(DESCRIPTION = (ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxx)(PORT = xxxx))(CONNECT_DATA=(SERVICE_NAME=xxxxx)));User Id=xxxxxxxx;Password=xxxxxxxx;";

        ///// <summary>
        ///// 实体类
        ///// </summary>
        //[Table("T_Students")]
        //public class Student : EntityBase
        //{
        //    [PrimaryKey]
        //    [Column]
        //    public string Id { get; set; }

        //    [Column("Name")]
        //    public string StudentName { get; set; }

        //    [Column]
        //    public int Age { get; set; }
        //}

        public class EntityCommon : EntityBase
        {
            [PrimaryKey]
            [Column]
            public string Id { get; set; }
            [Column]
            public string RemarkInfo { get; set; }
            [Column]
            public int DelFlag { get; set; }
            [Column]
            public DateTime CreateTime { get; set; }
            [Column]
            public DateTime UpdateTime { get; set; }
            [Column]
            public string Creater { get; set; }
            [Column]
            public string Updater { get; set; }

        }

        [Table("IDSBG_ECARD.B_DICTIONARY")]
        public class Dictionary : EntityCommon
        {
            [Column]
            public string Name { get; set; }
            [Column]
            public string Value { get; set; }
            [Column]
            public string Type { get; set; }
            [Column]
            public string BackupField_1 { get; set; }
            [Column]
            public string BackupField_2 { get; set; }
            [Column]
            public string BackupField_3 { get; set; }
            [Column]
            public string BackupField_4 { get; set; }
            [Column]
            public string BackupField_5 { get; set; }
        }

        public class WQSDbContext : OracleDbContext<WQSDbContext>
        {
            public WQSDbContext() : base(oracleConnStr)
            {
                this.OpenQueryCache = false;
                this.Log = context =>
                {
                    Console.WriteLine($"sql:{context.SqlStatement}");
                    Console.WriteLine($"time:{DateTime.Now}");
                };
            }
        }

        static void Main(string[] args)
        {
            using (var dbContext = new WQSDbContext())
            {
                string newId = Guid.NewGuid().ToString().Replace("-", "");

                Dictionary newDicItem = new Dictionary
                {
                    Id = newId,
                    RemarkInfo = "无",
                    DelFlag = 0,
                    CreateTime = DateTime.Now,
                    Creater = "test",
                    Name = "权限相关API",
                    Value = "PermissionAPI",
                    Type = "ApiType"
                };

                dbContext.Add<Dictionary>(newDicItem);

                var list = dbContext.Queryable<Dictionary>().Where(s => s.Id.Equals(newId)).Paging(1,2).Select().ToList();

            }
        }

        //static void Main(string[] args)
        //{
        //    using (var dbContext = new WQSDbContext())
        //    {
        //        dbContext.Add<Student>(new Student { Id = Guid.NewGuid().ToString().Replace("-", ""), StudentName = "吴前森", Age = 20 });

        //        string id = "55629da4b5df4579a92d901b8abc3781";
        //        string name = "吴前森";
        //        var l = dbContext.Queryable<Student>().Where(s => s.StudentName == name).Any();

        //        var list = dbContext.Queryable<Student>().Select(s => new { s.StudentName, s.Id }).Where(s => s.StudentName.Equals("吴前森") && s.StudentName.StartsWith("吴")).ToList();

        //    }
        //}

        //public class WQSDbContext : MysqlDbContext<WQSDbContext>
        //{
        //    public WQSDbContext() : base("User ID=root;Password=root;Host=localhost;Port=3306;Database=wqsblog;")
        //    {
        //        this.OpenQueryCache = false;
        //        this.Log = context =>
        //        {
        //            Console.WriteLine($"sql:{context.SqlStatement}");
        //            Console.WriteLine($"time:{DateTime.Now}");
        //        };
        //    }
        //}

    //    static void Main3(string[] args)
    //{
    //    using (var dbContext = new WQSDbContext())
    //    {
    //            //try
    //            //{
    //            //    dbContext.DBTransaction.Begin();
    //            //for (int i = 0; i < 10; i++)
    //            //{
    //            //    dbContext.Add<Student>(new Student { Id = Guid.NewGuid().ToString().Replace("-", ""), StudentName = $"吴前森{i}", Age = 20+i });
    //            //}


    //            //    int i = 0;
    //            //    int n = 5 / i;

    //            //    dbContext.Add<Student>(new Student { Id = Guid.NewGuid().ToString().Replace("-", ""), StudentName = "吴前森", Age = 20 });

    //            //    dbContext.DBTransaction.Commit();
    //            //}
    //            //catch
    //            //{
    //            //    dbContext.DBTransaction.Rollback();
    //            //}
    //            //finally
    //            //{
    //            //    dbContext.DBTransaction.End();
    //            //}


    //            //dbContext.ExecuteSql("update T_Students set Age = {0}", 20);

    //            //var list = dbContext.Queryable("select * from T_Students where Age = {0}", 20).Paging(1, 3).ToList<Student>();

    //            //string s = LambdaToSql.ConvertWhere<Student>(t => t.Age < 20 && t.Id.EndsWith("123"));

    //            //int count = dbContext.Delete<Student>(s => s.Age == 20);
    //            //dbContext.Add<Student>(new Student { Id = Guid.NewGuid().ToString().Replace("-", ""), StudentName = "吴前森", Age = 20 });
    //            //dbContext.Update<Student>(s => s.Age > 26, s => new { s.StudentName }, new Student { StudentName = "change" });

    //            //dbContext.Update<Student>(s => new { s.StudentName }, new Student { StudentName = "change" }).Where(s => s.Age < 26 && s.StudentName.StartsWith("吴")).Done();

    //            //var list = dbContext.Queryable("select * from T_Students where Age < {0}", 26).ToList<Student>();

    //            Student s1 = dbContext.Queryable<Student>().Select(s => new { s.StudentName, s.Id }).Where(s => s.Age > 20 && s.StudentName.StartsWith("吴")).ToOne();

    //            //list = dbContext.Queryable<Student>().Select(s => new { s.StudentName, s.Id }).Where(s => s.Age > 20 && s.StudentName.StartsWith("吴")).ToList();

    //            s1.Age = 100;

    //            dbContext.Update(s1);
    //    }

    //}

        //static void Main2(string[] args)
        //{
        //    DataTable dt = new DataTable();

        //    dt.Columns.Add(new DataColumn("Id"));
        //    dt.Columns.Add(new DataColumn("Age"));
        //    dt.Columns.Add(new DataColumn("Weight"));

        //    DataRow dr = dt.NewRow();
        //    dr["Id"] = "1234456";
        //    dr["Age"] = "25";
        //    dr["Weight"] = 25;

        //    dt.Rows.Add(dr);

        //    int i = (int)Convert.ChangeType(dr["Weight"], typeof(int));

        //    Student t = EntityFillAdapter<Student>.AutoFill(dr);

        //}

        //static void Main1(string[] args)
        //{
        //    LabelTarget labelBreak = Expression.Label();
        //    ParameterExpression loopIndex = Expression.Parameter(typeof(int), "index");

        //    BlockExpression block = Expression.Block(
        //        new[] { loopIndex },
        //        // 初始化loopIndex =1
        //        Expression.Assign(loopIndex, Expression.Constant(1)),
        //        Expression.Loop(
        //            Expression.IfThenElse(
        //                // if 的判断逻辑
        //                Expression.LessThanOrEqual(loopIndex, Expression.Constant(10)),
        //                // 判断逻辑通过的代码
        //                Expression.Block(
        //                    Expression.Call(
        //                        null,
        //                        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }),
        //                        Expression.Constant("Hello")),
        //                    Expression.PostIncrementAssign(loopIndex)),
        //                // 判断不通过的代码
        //                Expression.Break(labelBreak)
        //                ), labelBreak),
        //        loopIndex
        //    );

        //    // 将我们上面的代码块表达式
        //    var lambdaExpression = Expression.Lambda<Func<int>>(block);
        //    int i = lambdaExpression.Compile().Invoke();

        //    Console.WriteLine(lambdaExpression.ToString());

        //    Console.Read();
        //}
    }
}
